using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Algorithms.Numerical;
using Base;
using PetriDiagram.Model;
using Algorithms;

namespace PetriDiagram.Algorithms
{
    public partial class InvariantsForm : Form
    {
        public InvariantsForm( PetriNetModel net )
        {
            InitializeComponent();
            
            foreach( Place place in net.Places )
                placesListBox.Items.Add( place.Name );

            foreach( Transition transition in net.Transitions )
                transitionsListBox.Items.Add( transition.Name );
        }

        public static void ShowDialog( IntMatrix intMatrix, MarkingVector markingVector,
            PetriNetModel petriNetModel )
        {
            throw new Exception( "The method or operation is not implemented." );
        }

        private void InvariantsForm_Load( object sender, EventArgs e )
        {
        }

        private IntMatrix _matrix;

        public IntMatrix Matrix
        {
            get { return _matrix; }
            set { _matrix = value; }
        }

        private void button2_Click( object sender, EventArgs e )
        {
            this.Close();
        }

        private void button1_Click( object sender, EventArgs e )
        {

        }

        private void InvariantsForm_Shown( object sender, EventArgs e )
        {
            if( _matrix == null )
                return;

            textBox1.Text = "";
            textBox2.Text = "";

            IntMatrix pInvariants = Invariants.CalulatePInvariants( _matrix );

            if( pInvariants.Width > 0 )
            {
                StringBuilder sb = new StringBuilder();
                for( int i = 0; i < pInvariants.Width; ++i )
                {
                    sb.Append( "" + i + ": [ " );
                    for( int j = 0; j < pInvariants.Height; ++j )
                    {

                        sb.Append( pInvariants[ j, i ] + ", " );
                    }
                    sb.Remove( sb.Length - 2, 2 );
                    sb.Append( " ]\r\n" );
                }
                textBox1.Text = sb.ToString();
            }
            else
                textBox1.Text = "[No p-invariants]";

            IntMatrix tInvariants = Invariants.CalulateTInvariants( _matrix );

            if( tInvariants.Width > 0 )
            {
                StringBuilder sb = new StringBuilder();
                for( int i = 0; i < tInvariants.Width; ++i )
                {
                    sb.Append( "" + i + ": [ " );
                    for( int j = 0; j < tInvariants.Height; ++j )
                    {

                        sb.Append( tInvariants[ j, i ] + ", " );
                    }
                    sb.Remove( sb.Length - 2, 2 );
                    sb.Append( " ]\r\n" );
                }
                textBox2.Text = sb.ToString();
            }
            else
                textBox2.Text = "[No t-invariants]";
        }

    }
}